﻿Imports Entidad
Imports Utilitario
Imports System.Data.SqlClient
Public Class TablaMasivaDetalleDatos
    Inherits Utilitario.ConexionSQL
    Dim cmd As SqlCommand

    Public Function ConsultarTablaMasivaDetalle(ByVal tabla As String, ByVal nivel As String) As List(Of TablaMasivaDetalle)
        Try
            conectarSQL()
            Dim da As New SqlDataAdapter("", cnn)
            Dim ds As New DataSet
            Dim actIng As String = ""
            If nivel.Equals("0") Then
                actIng = " and utd.UTD_Actualiza='1'"
            End If
            If nivel.Equals("1") Then
                actIng = " and utd.UTD_Ingresa='1'"
            End If
            da.SelectCommand.CommandText = "SELECT td.TMD_Codigo, td.TMA_Codigo, td.TMD_Descripcion, td.TMD_TipoDato, td.TMD_TamanioMinimo, td.TMD_Key, td.TMD_Obligatorio, " & _
                " td.TMD_Fijo, td.TMD_ValorFijo, td.TMD_Activo " & _
                " FROM TablaMasivaDetalle td " & _
                " INNER JOIN UsuarioTablaMasivaDetalle utd ON utd.CIA = td.CIA and td.TMA_Codigo = utd.TMA_Codigo and utd.TMD_Codigo=td.TMD_Codigo " & IIf(actIng.Equals(""), "", actIng) & " and utd.USU_Codigo='" & Variables.Usuario.gUSU_Codigo & "' " & _
                " WHERE td.TMD_Activo='1' and td.CIA='" & Variables.Empresa.gCIA_Codigo & "' and td.TMA_Codigo = '" + tabla + "'" & _
                " ORDER BY TMD_Key desc, TMD_Descripcion"
            da.Fill(ds, "tablaMasivaDetalle")
            Dim campos As TablaMasivaDetalle
            Dim lista As New List(Of TablaMasivaDetalle)
            For Each dr As DataRow In ds.Tables(0).Rows
                campos = New TablaMasivaDetalle
                With campos
                    .gTMD_Codigo = If(dr("TMD_Codigo") Is DBNull.Value, "", dr("TMD_Codigo"))
                    .gTMA_Codigo = If(dr("TMA_Codigo") Is DBNull.Value, "", dr("TMA_Codigo"))
                    .gTMD_Descripcion = If(dr("TMD_Descripcion") Is DBNull.Value, "", dr("TMD_Descripcion"))
                    .gTMD_TipoDato = If(dr("TMD_TipoDato") Is DBNull.Value, "", dr("TMD_TipoDato"))
                    .gTMD_TamanioMinimo = If(dr("TMD_TamanioMinimo") Is DBNull.Value, "", dr("TMD_TamanioMinimo"))
                    .gTMD_Key = If(dr("TMD_Key") Is DBNull.Value, "", dr("TMD_Key"))
                    .gTMD_Obligatorio = If(dr("TMD_Obligatorio") Is DBNull.Value, "", dr("TMD_Obligatorio"))
                    .gTMD_Fijo = If(dr("TMD_Fijo") Is DBNull.Value, "", dr("TMD_Fijo"))
                    .gTMD_ValorFijo = If(dr("TMD_ValorFijo") Is DBNull.Value, "", dr("TMD_ValorFijo"))
                    .gTMD_Activo = If(dr("TMD_Activo") Is DBNull.Value, "", dr("TMD_Activo"))
                End With
                lista.Add(campos)
            Next

            Return lista
        Catch ex As Exception
            MsgBox(ex.Message)
            Return Nothing
        Finally
            desconectarSQL()
        End Try
    End Function

    Public Function TablaMasivaDetalleXCodigo(ByVal codigo As String) As TablaMasivaDetalle
        Try
            conectarSQL()
            Dim da As New SqlDataAdapter("", cnn)
            Dim ds As New DataSet
            da.SelectCommand.CommandText = "SELECT TMD_Codigo, TMA_Codigo, TMD_Descripcion, TMD_TipoDato, TMD_TamanioMinimo, TMD_Obligatorio, TMD_Fijo, TMD_ValorFijo, TMD_Activo " & _
                " FROM TablaMasivaDetalle " & _
                " WHERE TMD_Codigo = '" + codigo + "'"
            da.Fill(ds, "tablaMasivaDetalle")

            'Dim dr As SqlDataReader
            ' dr = cmd.ExecuteReader
            Dim tablaDetalle As TablaMasivaDetalle = Nothing

            For Each dr As DataRow In ds.Tables(0).Rows
                tablaDetalle = New TablaMasivaDetalle
                With tablaDetalle
                    .gTMD_Codigo = If(dr("TMD_Codigo") Is DBNull.Value, "", dr("TMD_Codigo"))
                    .gTMA_Codigo = If(dr("TMA_Codigo") Is DBNull.Value, "", dr("TMA_Codigo"))
                    .gTMD_Descripcion = If(dr("TMD_Descripcion") Is DBNull.Value, "", dr("TMD_Descripcion"))
                    .gTMD_TipoDato = If(dr("TMD_TipoDato") Is DBNull.Value, "", dr("TMD_TipoDato"))
                    .gTMD_TamanioMinimo = If(dr("TMD_TamanioMinimo") Is DBNull.Value, "", dr("TMD_TamanioMinimo"))
                    .gTMD_Key = If(dr("TMD_Key") Is DBNull.Value, "", dr("TMD_Key"))
                    .gTMD_Obligatorio = If(dr("TMD_Obligatorio") Is DBNull.Value, "", dr("TMD_Obligatorio"))
                    .gTMD_Fijo = If(dr("TMD_Fijo") Is DBNull.Value, "", dr("TMD_Fijo"))
                    .gTMD_ValorFijo = If(dr("TMD_ValorFijo") Is DBNull.Value, "", dr("TMD_ValorFijo"))
                    .gTMD_Activo = If(dr("TMD_Activo") Is DBNull.Value, "", dr("TMD_Activo"))
                End With
            Next
            Return tablaDetalle

        Catch ex As Exception
            MsgBox(ex.Message)
            Return Nothing
        Finally
            desconectarSQL()
        End Try
        Return Nothing
    End Function
End Class
